package org.chnxi.study.design.single;

import java.io.*;
import java.lang.reflect.Constructor;
import java.util.concurrent.TimeUnit;

public class LazySingleTest {
    public static void main(String[] args) throws Exception {
//        new Thread(()->{
//            Constructor<LazySingle> declaredConstructor = null;
//            try {
//                declaredConstructor = LazySingle.class.getDeclaredConstructor(null);
//                declaredConstructor.setAccessible(true);
//                LazySingle lazySingle = declaredConstructor.newInstance();
//                System.out.println(Thread.currentThread().getName()+";lazySingle.hashcode="+lazySingle.hashCode());
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }, "T-1").start();

        new Thread(()->{
            try{
                LazySingle instance = LazySingle.getInstance();
                System.out.println(Thread.currentThread().getName()+";instance.hashcode="+instance.hashCode());

                ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("/Users/admin/tmp/tmpfile.data"));
                outputStream.writeObject(instance);
                outputStream.flush();
                outputStream.close();

                ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(new File("/Users/admin/tmp/tmpfile.data")));
                LazySingle s2 = (LazySingle)inputStream.readObject();
                System.out.println(Thread.currentThread().getName()+";instance.hashcode="+s2.hashCode());
                inputStream.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }, "T-2").start();
    }
}
